package grinch.edu.probtheory;


import java.util.ArrayList;
import java.util.List;

import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;


import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class ProbabilitytheoryActivity extends Activity {
	
	private LeastSquaresGraph leastSquaresGraph;
	private BarGraph barGraph;
	
	private List<View> mPages;
	private View mPage1;
    private View mPage2;
    private ViewPager mPager;
    private TitlePageIndicator mTitleIndicator;
    
    private void initUi() {
        LayoutInflater inflater = LayoutInflater.from(this);
        mPages = new ArrayList<View>();

        mPage1 = inflater.inflate(R.layout.randoms, null);
        mPage1.setTag(getString(R.string.textTab1));
        
        mPage2 = inflater.inflate(R.layout.bargraph, null);
        mPage2.setTag(getString(R.string.textTab2));
        
        mPages.add(mPage1);
        mPages.add(mPage2);
        
        MainPageAdapter adapter = new MainPageAdapter(mPages);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(0);

        mTitleIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
        mTitleIndicator.setViewPager(mPager);
        mTitleIndicator.setCurrentItem(0);
        mTitleIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 1) {
					View view = mPages.get(arg0);
					if (!barGraph.getTag().toString().equals("1")){
						barGraph.setTag("1");
		                ((RelativeLayout)view).addView(barGraph);
					}else{
						barGraph.invalidate();
					}
					
					
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    public class MainPageAdapter extends PagerAdapter implements TitleProvider {

        private List<View> mPages;

        public MainPageAdapter(List<View> pPages) {
                mPages = pPages;
        }

        @Override
        public Object instantiateItem(View pCollection, int pPosition) {
                View view = mPages.get(pPosition);
                if (pPosition == 0) {
                	((RelativeLayout)view).addView(leastSquaresGraph);
                }
                ((ViewPager) pCollection).addView(view, 0);
                return view;
        }

        @Override
        public void destroyItem(View pCollection, int pPosition, Object pView) {
                ((ViewPager) pCollection).removeView((View) pView);
        }

        @Override
        public int getCount() {
                return mPages.size();
        }

        @Override
        public boolean isViewFromObject(View pView, Object pObject) {
                return pView.equals(pObject);
        }

        @Override
        public void finishUpdate(View pView) {
        	
        }

        @Override
        public void restoreState(Parcelable pParcelable, ClassLoader pLoader) {
        }

        @Override
        public Parcelable saveState() {
                return null;
        }

        @Override
        public void startUpdate(View pView) {
        	
        }

        /**
         * For TitleProvider
         */
        public String getTitle(int pPosition) {
                return (String) (mPages.get(pPosition).getTag());
        }

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        leastSquaresGraph  = new LeastSquaresGraph(this);
        leastSquaresGraph.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				leastSquaresGraph.invalidate();
			}
		});
        barGraph = new BarGraph(this,leastSquaresGraph);
        barGraph.setTag("0");
        barGraph.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				barGraph.invalidate();	
			}
		});
        
    
        initUi();
        
        
        RadioGroup methodRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        methodRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {	
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
		        RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
		        // This puts the value (true/false) into the variable
		        boolean isChecked = checkedRadioButton.isChecked();
		        if (isChecked)
		        {		        
		           if (checkedRadioButton.getTag().toString().equals("gauss")) 
		            	leastSquaresGraph.setPlotMethod(RandomExtra.METHOD_NORMAL);
		            if (checkedRadioButton.getTag().toString().equals("laplass")) 
		            	leastSquaresGraph.setPlotMethod(RandomExtra.METHOD_LAPLASS);
		            if (checkedRadioButton.getTag().toString().equals("uniform")) 
		            	leastSquaresGraph.setPlotMethod(RandomExtra.METHOD_UNIFORM);		        			    
		        }	
		        
		        leastSquaresGraph.invalidate();
		        if (barGraph.getTag().toString().equals("1")) barGraph.invalidate();
			}
		});
        RadioGroup stateRadioGroup = (RadioGroup) findViewById(R.id.radioGroup2);
        stateRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
		        // This puts the value (true/false) into the variable
		        boolean isChecked = checkedRadioButton.isChecked();
		        if (isChecked)
		        {
		            if (checkedRadioButton.getTag().toString().equals("10")) leastSquaresGraph.setPlotState(LeastSquaresGraph.STATE_10);
		            if (checkedRadioButton.getTag().toString().equals("100")) leastSquaresGraph.setPlotState(LeastSquaresGraph.STATE_100);
		            if (checkedRadioButton.getTag().toString().equals("1000")) leastSquaresGraph.setPlotState(LeastSquaresGraph.STATE_1000);
		        }
		        leastSquaresGraph.invalidate();	
		        if (barGraph.getTag().toString().equals("1")) barGraph.invalidate();
			}
		});
        final TextView mvIndicator = (TextView) findViewById(R.id.textView4);
        final SeekBar sb1 = (SeekBar) findViewById(R.id.seekBar1);
        sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {	
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				float mw = (progress-50)/10;
				mvIndicator.setText(String.valueOf(mw));
				leastSquaresGraph.setMethodExpected(mw);
				leastSquaresGraph.invalidate();
				if (barGraph.getTag().toString().equals("1")) barGraph.invalidate();
			}
		});
        final TextView dispIndicator = (TextView) findViewById(R.id.textView6);
        final SeekBar sb2 = (SeekBar) findViewById(R.id.seekBar2);
        sb2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {	
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				float disp = (progress/(5));
				//float disp = (float) 0.33;
				dispIndicator.setText(String.valueOf(disp));
				//dispIndicator.setText(String.valueOf(0.33));
				leastSquaresGraph.setMethodDispersion(disp);
				leastSquaresGraph.invalidate();
				if (barGraph.getTag().toString().equals("1")) barGraph.invalidate();
			}
		});
    }
}